﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XmlServer.ServiceContracts;
using XmlServer.ServiceImplementation;


namespace MathOperator
{
    public class Math : IOperator
    {

        public const string SERVER_NAME = "Math Operator";
        public const string SERVER_VERSION = "1.0";

        public string Name
        {
            get { return SERVER_NAME; }
        }

        public string Version
        {
            get { return SERVER_VERSION; }
        }

        public string FullName
        {
            get { return String.Format("{0}_{1}", SERVER_NAME, Version); }
        }

        public string Description
        {
            get { return "This operator will perform the math operation on the 2 nubmers and return the result."; }
        }

        public List<IFact> Facts
        {
            get
            {
                return new List<IFact>
                {
                    new Fact { Name = "FirstNumber", IsRequired = true, Description = "The first value to use in the math operation." },
                    new Fact { Name = "SecondNumber", IsRequired = true, Description = "The second value to use in the math operation." },
                    new Fact { Name = "Operation", IsRequired = true, Description = "The math operation to perform: add, subtract, divide, multiple" }                    
                };
            }
        }


        public IResponse ProcessRequest(IRequest request)
        {
            Response response = new Response();
            response.Request = request;     // return the original request object
            response.Success = true;        // assume a success

            try
            {
                // get the "Facts" we need from the request
                var firstNumber = XmlServer.Helper.Utilities.GetFact<int>(request.Facts, "FirstNumber");
                var secondNumber = XmlServer.Helper.Utilities.GetFact<int>(request.Facts, "SecondNumber");
                var operation = XmlServer.Helper.Utilities.GetFact(request.Facts, "Operation");


                double calculation = 0;
                if (operation.Equals("add", StringComparison.InvariantCultureIgnoreCase))
                {
                    calculation = firstNumber + secondNumber;
                }
                else if (operation.Equals("subtract", StringComparison.InvariantCultureIgnoreCase))
                {
                    calculation = firstNumber - secondNumber;
                }
                else if (operation.Equals("divide", StringComparison.InvariantCultureIgnoreCase))
                {
                    calculation = firstNumber / secondNumber;
                }
                else if (operation.Equals("multiple", StringComparison.InvariantCultureIgnoreCase))
                {
                    calculation = firstNumber * secondNumber;
                }
                else
                {
                    response.Facts = XmlServer.Helper.Utilities.CreateException("Invalid input fact 'Operation'.  The fact must be add, subtract, divide, or multuple");
                    response.Success = false;
                }

                // return the success response
                if (response.Success)
                {
                    response.Facts = new List<IFact>()
                    {
                        new Fact {  Name = "Calculation", Value = calculation.ToString("F") }
                    };
                }  

            }
            catch (Exception ex)
            {
                response.Facts = XmlServer.Helper.Utilities.CreateException(ex);
                response.Success = false;
            }

            response.StopProcessing = System.Environment.TickCount;
            return response;

        }

    }
}
